In [1]:
import plotly.offline as pyo

from plotly.graph_objs import *

import chart_studio.plotly as py

import pandas as pd
from pandas import DataFrame
In [2]:
pyo.offline.init_notebook_mode()
In [3]:
LifePopGDP = pd.read_csv(r"../Data/GapminderData.csv", index_col = 0)
LifePopGDP.head()
Out[3]:
Continent Country Life Expectancy GDP per Capita Population
0 Africa Algeria 70.874 12779.0 36488669.0
1 Africa Angola 51.498 7230.0 20039259.0
2 Africa Benin 59.165 1685.0 9775152.0
3 Africa Botswana 47.152 14905.0 2030287.0
5 Africa Burundi 53.637 737.0 8899077.0
In [5]:
LifePopGDP['text'] = LifePopGDP.apply(lambda x: 
        "<b>{}</b><br>Life Expectancy: {:.0f} years<br>GDP/Capita: ${:,.0f}<br>Population: {:,.0f}".format(x['Country'], x['Life Expectancy'], x['GDP per Capita'], x['Population']), axis = 1)
LifePopGDP.head()
Out[5]:
Continent Country Life Expectancy GDP per Capita Population text
0 Africa Algeria 70.874 12779.0 36488669.0 <b>Algeria</b><br>Life Expectancy: 71 years<br...
1 Africa Angola 51.498 7230.0 20039259.0 <b>Angola</b><br>Life Expectancy: 51 years<br>...
2 Africa Benin 59.165 1685.0 9775152.0 <b>Benin</b><br>Life Expectancy: 59 years<br>G...
3 Africa Botswana 47.152 14905.0 2030287.0 <b>Botswana</b><br>Life Expectancy: 47 years<b...
5 Africa Burundi 53.637 737.0 8899077.0 <b>Burundi</b><br>Life Expectancy: 54 years<br...
In [6]:
LifePopGDP.loc[0, 'text']
Out[6]:
'<b>Algeria</b><br>Life Expectancy: 71 years<br>GDP/Capita: $12,779<br>Population: 36,488,669'
In [7]:
continents = LifePopGDP['Continent'].unique()
continents
Out[7]:
array(['Africa', 'Asia', 'Europe', 'North America', 'Oceania',
       'South America'], dtype=object)
In [8]:
traces = []
for c in continents:
    traces.append({'type' : 'scatter',
                   'mode' : 'markers',
                   'name' : c,
                   'hoverinfo' : 'text',
                  'x' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'GDP per Capita'],
                  'y' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Life Expectancy'],
                  'text' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'text'],
                  'marker' : {'size' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Population'],
                             'sizeref' : 500000,
                             'sizemode' : 'area'}})
In [9]:
pyo.iplot(traces)
In [10]:
traces = []
for c in continents:
    traces.append({'type' : 'scatter',
                   'mode' : 'markers',
                   'name' : c,
                   'hoverinfo' : 'text',
                  'x' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'GDP per Capita'],
                  'y' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Life Expectancy'],
                  'text' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'text'],
                  'marker' : {'size' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Population'],
                             'sizeref' : 500000,
                              'sizemode' : 'area',
                             'sizemin' : 2.5}})
pyo.iplot(traces)
In [12]:
layout = {'title' : 'Life Expectancy and GDP per capita',
         'xaxis' : {'title' : 'GDP per capita at PPP ($)',
                   'range' : [LifePopGDP['GDP per Capita'].min() * 0.95, 
                              LifePopGDP['GDP per Capita'].max() * 1.05]},
         'yaxis' : {'title' : 'Life expectancy (years)',
                   'range' : [LifePopGDP['Life Expectancy'].min() * 0.95, 
                              LifePopGDP['Life Expectancy'].max() * 1.05]},
         'hovermode' : 'closest'}
fig = Figure(data=traces, layout=layout)
pyo.iplot(fig)
In [13]:
difference = LifePopGDP['GDP per Capita'].max() - LifePopGDP['GDP per Capita'].min()
fig['layout']['xaxis'].update({'range' : [LifePopGDP['GDP per Capita'].min() - (difference * 0.05), 
                                          LifePopGDP['GDP per Capita'].max() * 1.05]})
pyo.iplot(fig)
In [ ]: